<?php

namespace application\api\controller;

use app\api\model\AdminModel;

use think\Request;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class Login extends Cross
{
    public function login(Request $request){
        $username = $request->param('username');
        $password = $request->param('password');
        $admin = new AdminModel();
        $info =  $admin->where('username',$username)->find();
        if (!$info) {
            return json(['code'=>0,'msg'=>'账号不存在']);
        }
        //echo md5($password);
        if ($info['password'] != md5($password)){
            return json(['code'=>0,'msg'=>'密码错误']);
        }
        //jwt 命令行composer require firebase/php-jwt

        $key = 'api123456';
        $payload = [
            'iss' => 'http://www.code.test',
            'aud' => 'http://www.code.test',
            'iat' => time(),
            'nbf' => time(),
            'aid' => $info['id']
        ];
//        $jwt = JWT::encode($payload, $key, 'HS256');
//        return json(['code'=>0,'msg'=>'登录成功','token'=>$token]);
       return json(['code'=>1,'msg'=>'登录成功']);
    }
}
